package com.wjk.kylin.mall.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjk.kylin.id.core.uid.UidGenerator;
import com.wjk.kylin.id.core.util.IdUtil;
import com.wjk.kylin.lock.annotation.KylinLock;
import com.wjk.kylin.mall.common.core.bo.admin.UserNameBO;
import com.wjk.kylin.mall.common.core.constant.Constants;
import com.wjk.kylin.mall.common.core.dto.admin.UserNameInDTO;
import com.wjk.kylin.mall.common.core.dto.base.BaseOutDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataIdInDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataOutDTO;
import com.wjk.kylin.mall.admin.beanmapper.UserEntityMapper;
import com.wjk.kylin.mall.admin.bo.UserBO;
import com.wjk.kylin.mall.admin.dto.UserInDTO;
import com.wjk.kylin.mall.admin.entity.UserEntity;
import com.wjk.kylin.mall.admin.mapper.UserMapper;
import com.wjk.kylin.mall.admin.service.IUserService;
import com.wjk.kylin.mall.common.redis.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author wangjinkui
 * @since 2021-11-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    @Qualifier("cachedUidGenerator")
    private UidGenerator uidGenerator;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 根据id查询数据
     *
     * @param inDTO
     * @return
     */
    @Override
    public BaseOutDTO getById(DataIdInDTO inDTO) {
        UserBO userBO = UserEntityMapper.INSTANCE.entity2BO(baseMapper.selectById(inDTO.getId()));
        return DataOutDTO.builder().data(userBO).build();
    }

    @Override
    @KylinLock(keys = "#inDTO.username")
    @Transactional
    //kylin-mall-admin:com.wjk.kylin.mall.admin.service.impl.UserServiceImpl.add#wjk
    public BaseOutDTO add(UserInDTO inDTO) {
        UserEntity entity = UserEntityMapper.INSTANCE.dto2Entity(inDTO);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        //TODO 初始化密码
        entity.setPassword(passwordEncoder.encode(Constants.INIT_PASSWORD));
        //百度uid
        entity.setId(uidGenerator.getUID());
        if (super.save(entity)) {
            return BaseOutDTO.success();
        }
        return BaseOutDTO.error("操作失败，请重试！");
    }

    @Override
    public BaseOutDTO loadUserByUsername(UserNameInDTO inDTO) {
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(inDTO.getUsername()), UserEntity::getUsername, inDTO.getUsername());
        wrapper.select(UserEntity::getId, UserEntity::getNickname, UserEntity::getUsername);

        UserNameBO userNameBO = UserEntityMapper.INSTANCE.entity2UserNameBO(this.getOne(wrapper));

        return DataOutDTO.builder().data(userNameBO).build();
    }

    @Override
    public void insertBatchSelective(List<UserEntity> list) {
        baseMapper.insertBatchSelective(list);
    }

    @Override
    public void updateBatchByPrimaryKeySelective(List<UserEntity> list) {
        baseMapper.updateBatchByPrimaryKeySelective(list);
    }

}
